/*******************************************************************************
  SYS PORTS Static Functions for PORTS System Service

  Company:
    Microchip Technology Inc.

  File Name:
    plib_gpio.c

  Summary:
    GPIO function implementations for the GPIO PLIB.

  Description:
    The GPIO PLIB provides a simple interface to manage peripheral
    input-output controller.

*******************************************************************************/

//DOM-IGNORE-BEGIN
/*******************************************************************************
* Copyright (C) 2019 Microchip Technology Inc. and its subsidiaries.
*
* Subject to your compliance with these terms, you may use Microchip software
* and any derivatives exclusively with Microchip products. It is your
* responsibility to comply with third party license terms applicable to your
* use of third party software (including open source software) that may
* accompany Microchip software.
*
* THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
* EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED
* WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A
* PARTICULAR PURPOSE.
*
* IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
* INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
* WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS
* BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE
* FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN
* ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
* THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
*******************************************************************************/
//DOM-IGNORE-END

#include "plib_gpio.h"



/******************************************************************************
  Function:
    GPIO_Initialize ( void )

  Summary:
    Initialize the GPIO library.

  Remarks:
    See plib_gpio.h for more details.
*/
void GPIO_Initialize ( void )
{

    /* PORTA Initialization */
    LATASET = 0x0480U;
    ANSELACLR = 0xd100U; /* Digital Mode Enable */
    TRISASET = 0x0100U;  //RA8
    /* PORTB Initialization */
    LATBSET = 0xfc00U;
    ANSELBCLR = 0x28EU; /* Digital Mode Enable */
    TRISBSET = 0x0082U;  //FO�ź�
    TRISBCLR = 0x000CU;        
    /* PORTC Initialization */
    LATC = 0x40U; /* Initial Latch Value */
    TRISCCLR = 0x40U; /* Direction Control */
//    TRISCSET = 0x800;  //RC11����FO
//    ANSELCCLR = 0x1c40U; /* Digital Mode Enable */
    TRISCSET = 0x802;
    ANSELCCLR = 0x1c42U; /* Digital Mode Enable */
    /* PORTD Initialization */
    LATD = 0xf11EU; /* Initial Latch Value */
    TRISDCLR = 0xf100U;  /* Direction Control */
    ANSELDCLR = 0xc000U; /* Digital Mode Enable */
    TRISDSET = 0x8000U;  //RD15  
    /* PORTE Initialization */
    ANSELECLR = 0xf103U; /* Digital Mode Enable */

    /* Change Notice Enable */
    //CNCONESET = _CNCONE_ON_MASK;
    //PORTE;
    //IEC1SET = _IEC1_CNEIE_MASK;
    
    /* PORTF Initialization */
    LATF = 0x2A0U; /* Initial Latch Value */
    TRISFCLR = 0x32A0U; /* Direction Control */
    ANSELFCLR = 0x32A0U; /* Digital Mode Enable */
    /* PORTG Initialization */
    LATG = 0x0400U; /* Initial Latch Value */
    TRISGCLR = 0x0400U; /* Direction Control */
    TRISGSET = 0x1C0U;      //RG6 RG7 RG8
    ANSELGCLR = 0x7c0U; /* Digital Mode Enable */
    
    /* Unlock system for PPS configuration */
    SYSKEY = 0x00000000U;
    SYSKEY = 0xAA996655U;
    SYSKEY = 0x556699AAU;
    CFGCONbits.IOLOCK = 0U;
    
//boot
    /* PPS Input Remapping */
//    SDI3R = 9;//13;
//    SDI2R = 12;
//    SDI5R = 13;//9;
//    SDI4R = 9;
//    SDI6R = 8;
    U1RXR = 6;   //RPC1--TRX
    U3RXR = 11;
    U4RXR = 11;
    U5RXR = 9;

    /* PPS Output Remapping */
//    RPA14R = 16;//14;
//    RPE0R = 4;
//    RPG1R = 14;//16;
//    RPC8R = 14;//16;
//    RPC7R = 15;
//    RPC9R = 15;
//    RPB5R = 17;
//    RPE14R = 17;
//    RPB9R = 16;//14;
    RPB4R = 1;   //RPB4--U1TX
    RPF0R = 1;   //U3TX--TX
    RPG0R = 2;   //U4TX--TX2
    RPC10R = 11; //U5TX--TX3

    
//app
//    /* PPS Input Remapping */
//    QEA1R = 10;  //RPG6--RG6--M0_A
//    QEB1R = 10;  //RPG7--RG7--M0_B
    SDI3R = 9;   //RPD5--RD5--SDI3
    //SDI1R = 2;   //RPB4--RB4--SDI1
    SDI2R = 12;  //RPE1--RE1--SDI2
    SDI5R = 13;  //RPA15--SDI5
    SDI4R = 9;   //RPD6--SDI4
    SDI6R = 8;   //RPE15--SDI6
//    U2RXR = 8;   //RPA12--TRX
//    U3RXR = 11;  //RPF1--RX
//    U5RXR = 9;   //RPC13--RX3
//    U4RXR = 11;  //RPG12--RX2
//    //FLT2R = 2;   //RPB1--M0-FO
//    INT3R = 2;  //RPB1
//    QEA5R = 4;  //RPB7--RB7--M1_A
//    QEB5R = 5;  //RPA8--RA8--M1_B
//
//    /* PPS Output Remapping */
   // RPA8R = 3;   //SD01
    RPC8R = 14;  //SDO3
    RPE0R = 4;   //SDO2
    RPG1R = 14;  //SCK3
    RPC7R = 15;  //SCK4
    RPA14R = 16;  //SDO5
    RPC9R = 15;  //SDO4
    RPB9R = 16;  //SCK5
    RPE14R = 17; //SDO6
    RPB5R = 17;  //SCK6
//    RPF0R = 1;   //U3TX--TX
//    RPC10R = 11; //U5TX--TX3
//    RPG0R = 2;   //U4TX--TX2
//    RPC6R = 11;   //TX6  debug
    
    /* Lock back the system after PPS configuration */
    CFGCONbits.IOLOCK = 1U;
    SYSKEY = 0x00000000U;
    
}

// *****************************************************************************
// *****************************************************************************
// Section: GPIO APIs which operates on multiple pins of a port
// *****************************************************************************
// *****************************************************************************

// *****************************************************************************
/* Function:
    uint32_t GPIO_PortRead ( GPIO_PORT port )

  Summary:
    Read all the I/O lines of the selected port.

  Description:
    This function reads the live data values on all the I/O lines of the
    selected port.  Bit values returned in each position indicate corresponding
    pin levels.
    1 = Pin is high.
    0 = Pin is low.

    This function reads the value regardless of pin configuration, whether it is
    set as as an input, driven by the GPIO Controller, or driven by a peripheral.

  Remarks:
    If the port has less than 32-bits, unimplemented pins will read as
    low (0).
    Implemented pins are Right aligned in the 32-bit return value.
*/
uint32_t GPIO_PortRead(GPIO_PORT port)
{
    return (*(volatile uint32_t *)(&PORTA + (port * 0x40U)));
}

// *****************************************************************************
/* Function:
    void GPIO_PortWrite (GPIO_PORT port, uint32_t mask, uint32_t value);

  Summary:
    Write the value on the masked I/O lines of the selected port.

  Remarks:
    See plib_gpio.h for more details.
*/
void GPIO_PortWrite(GPIO_PORT port, uint32_t mask, uint32_t value)
{
    *(volatile uint32_t *)(&LATA + (port * 0x40U)) = (*(volatile uint32_t *)(&LATA + (port * 0x40U)) & (~mask)) | (mask & value);
}

// *****************************************************************************
/* Function:
    uint32_t GPIO_PortLatchRead ( GPIO_PORT port )

  Summary:
    Read the latched value on all the I/O lines of the selected port.

  Remarks:
    See plib_gpio.h for more details.
*/
uint32_t GPIO_PortLatchRead(GPIO_PORT port)
{
    return (*(volatile uint32_t *)(&LATA + (port * 0x40U)));
}

// *****************************************************************************
/* Function:
    void GPIO_PortSet ( GPIO_PORT port, uint32_t mask )

  Summary:
    Set the selected IO pins of a port.

  Remarks:
    See plib_gpio.h for more details.
*/
void GPIO_PortSet(GPIO_PORT port, uint32_t mask)
{
    *(volatile uint32_t *)(&LATASET + (port * 0x40U)) = mask;
}

// *****************************************************************************
/* Function:
    void GPIO_PortClear ( GPIO_PORT port, uint32_t mask )

  Summary:
    Clear the selected IO pins of a port.

  Remarks:
    See plib_gpio.h for more details.
*/
void GPIO_PortClear(GPIO_PORT port, uint32_t mask)
{
    *(volatile uint32_t *)(&LATACLR + (port * 0x40U)) = mask;
}

// *****************************************************************************
/* Function:
    void GPIO_PortToggle ( GPIO_PORT port, uint32_t mask )

  Summary:
    Toggles the selected IO pins of a port.

  Remarks:
    See plib_gpio.h for more details.
*/
void GPIO_PortToggle(GPIO_PORT port, uint32_t mask)
{
    *(volatile uint32_t *)(&LATAINV + (port * 0x40U))= mask;
}

// *****************************************************************************
/* Function:
    void GPIO_PortInputEnable ( GPIO_PORT port, uint32_t mask )

  Summary:
    Enables selected IO pins of a port as input.

  Remarks:
    See plib_gpio.h for more details.
*/
void GPIO_PortInputEnable(GPIO_PORT port, uint32_t mask)
{
    *(volatile uint32_t *)(&TRISASET + (port * 0x40U)) = mask;
}

// *****************************************************************************
/* Function:
    void GPIO_PortOutputEnable ( GPIO_PORT port, uint32_t mask )

  Summary:
    Enables selected IO pins of a port as output(s).

  Remarks:
    See plib_gpio.h for more details.
*/
void GPIO_PortOutputEnable(GPIO_PORT port, uint32_t mask)
{
    *(volatile uint32_t *)(&TRISACLR + (port * 0x40U)) = mask;
}




/*******************************************************************************
 End of File
*/
